package com.ygg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.ygg.cmn.client.DictFeignClient;
import com.ygg.model.hosp.Hospital;
import com.ygg.repository.HospitalRepository;
import com.ygg.service.HospitalService;
import com.ygg.vo.hosp.HospitalQueryVo;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Resource
    HospitalRepository hospitalRepository;

    @Resource
    DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        //判断是否存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (null != targetHospital) {
            hospital.setId(targetHospital.getId());
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHosCode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }


    @Override
    public Page<Hospital> list(Integer page, Integer pageSize, HospitalQueryVo vo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //0为第一页
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);

        Hospital hospital = BeanUtil.copyProperties(vo, Hospital.class);

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> all = hospitalRepository.findAll(example, pageable);

        List<Hospital> content = all.getContent();

        content.forEach(item -> {
            this.packHospital(item);
        });


        return all;

    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospitalRepository.save(hospital);


    }

    @Override
    public Map<String, Object> show(String id) {
        Map<String, Object> result = new HashMap<>();

        Hospital hospital = hospitalRepository.findById(id).get();
        this.packHospital(hospital);
        result.put("hospital", hospital);

        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;

    }

    @Override
    public String getHospName(String hoscode) {
        return this.getByHosCode(hoscode).getHosname();
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();

        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        this.packHospital(hospital);
        result.put("hospital", hospital);

        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    private void packHospital(Hospital item) {
        String name = dictFeignClient.getName("Hostype", item.getHostype());
        item.getParam().put("hospitalName", name);

        //省
        String provinceName = dictFeignClient.getName(item.getProvinceCode());
        item.getParam().put("provinceName", provinceName);

        //市
        String cityName = dictFeignClient.getName(item.getCityCode());
        item.getParam().put("cityName", cityName);
        //区
        String districtName = dictFeignClient.getName(item.getDistrictCode());
        item.getParam().put("districtName", districtName);

        item.getParam().put("address", provinceName + cityName + districtName);
    }
}
